﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Medianamik.Core.Interfaces;
using Medianamik.Core.Security;
using Medianamik.UI.Controls;
using Telerik.Web.UI;

namespace Medianamik.UI
{
    public class GridNameColumnTemplate : ITemplate
    {
        #region Fields

        private readonly bool _navigate;
        private readonly CommandEventHandler _navigateHandler;
        private NodeTypeIcon _nodeTypeIcon;
        private LinkButton _nameButton;
        private Literal _name;

        #endregion

        #region .ctors

        public GridNameColumnTemplate(bool navigate, CommandEventHandler navigateHandler)
        {
            _navigate = navigate;
            _navigateHandler = navigateHandler;
        }

        #endregion

        #region Events

        void NodeTypeIconDataBinding(object sender, EventArgs e)
        {
            var container = (GridDataItem)_nodeTypeIcon.NamingContainer;
            var node = ((INode)container.DataItem);
            _nodeTypeIcon.NodeTypeId = node.NodeTypeID;
        }

        void NameButtonDataBinding(object sender, EventArgs e)
        {
            var container = (GridDataItem)_nodeTypeIcon.NamingContainer;
            var node = ((INode)container.DataItem);

            _nameButton.Enabled = node.CanHaveChildNodes 
                && Roles.UserHasRight(node, SecurityLevels.Read);
            _nameButton.Text = node.DisplayName;
            _nameButton.CommandArgument = node.ID.ToString();
        }

        void NameDataBinding(object sender, EventArgs e)
        {
            var container = (GridDataItem)_nodeTypeIcon.NamingContainer;
            var node = ((INode)container.DataItem);

            _name.Text = node.DisplayName;
        }

        #endregion

        #region Properties

        public LinkButton NameButton
        {
            get { return _nameButton; }
        }

        #endregion

        #region Methods

        public void InstantiateIn(Control container)
        {
            #region NodeTypeIcon

            _nodeTypeIcon = new NodeTypeIcon();
            container.Controls.Add(_nodeTypeIcon);
            _nodeTypeIcon.DataBinding += NodeTypeIconDataBinding;

            #endregion

            #region Name

            var p = new HtmlGenericControl("p");
            container.Controls.Add(p);

            if (_navigate)
            {
                _nameButton = new LinkButton();
                _nameButton.Command += _navigateHandler;
                p.Controls.Add(_nameButton);
                _nameButton.DataBinding += NameButtonDataBinding;
            }
            else
            {
                _name = new Literal();
                p.Controls.Add(_name);
                _name.DataBinding += NameDataBinding;
            }

            #endregion
        }

        #endregion
    }
}
